/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 26727
 * Date: 2024-05-07
 * Time: 20:48
 */
//懒汉模式实现单例模式
class SingletonLazy {
    volatile private static SingletonLazy instance = null; //先置为空

    public static SingletonLazy getInstance() { //只有调用这个才会创建对象
        if(instance == null) {
            synchronized (SingletonLazy.class) {
                if (instance == null) {
                    instance = new SingletonLazy();
                }
            }
        }
        return instance;
    }
    private SingletonLazy() { }
}

public class ThreadDemo15 {
    public static void main(String[] args) {
        SingletonLazy s1 = SingletonLazy.getInstance();
        SingletonLazy s2 = SingletonLazy.getInstance();
        System.out.println(s1 == s2);
    }
}

